#include "thread"
#include "iostream"
#include "queue"
#include "functional"
#include "mutex"
#include "condition_variable"

// 简单版本

std::mutex mtx;
std::condition_variable cond;

void threadFunc(std::queue<int>& q) {
    while (true) {
        if (!q.empty()) {
            std::unique_lock<std::mutex> lock(mtx);
            cond.wait(lock, [q]()->bool{ return !q.empty(); });

            int param = q.front();
            q.pop();
            std::cout << "param: " << param << std::endl;
        }
    }
}

void jobDispatch(std::queue<int>& q) {
    for (int i = 0; i < 1000; i++) {
        q.push(i);
    }
    cond.notify_all();
}

int main1() {
    std::queue<int> q1;
    std::vector<std::thread> ths;
    for (int i = 0; i < 10; ++i) {
        ths.emplace_back(threadFunc, std::ref(q1));  // std::ref 可以创建一个引用包装器，将对象的引用传递给函数
    }
    jobDispatch(q1);

    for (auto &th : ths) {
        th.join();
    }
}




#include "xp_threadpool.h"
#include <iostream>

using namespace std;

void threadFunc(int a) {
    cout << "a=" << a << endl;
}

class A {
public:
    A() = default;
    int run(int a, int b) {
        return a + b;
    }
};

class T {
    void operator()() {
        int a = 1;
    }
};

int main() {
    ThreadPool p1;
    p1.init(10);

    T t;

    p1.exec<void(int), int>(threadFunc, 200);

    A a1;
    auto fu1 = p1.exec(std::bind(&A::run, &a1, std::placeholders::_1, std::placeholders::_2), 10, 20);
    int ret = fu1.get();
    std::cout << "res:" << ret << std::endl;
//
//    p1.waitDone();
    return 0;
}


